package cn.yzking.studentmanagement.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupMenu;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import java.util.Set;

import cn.yzking.studentmanagement.R;
import cn.yzking.studentmanagement.Student;
import cn.yzking.studentmanagement.StudentDetailActivity;
import cn.yzking.studentmanagement.adapter.StudentAdapter;
import cn.yzking.studentmanagement.databinding.FragmentIndexBinding;
import cn.yzking.studentmanagement.utils.MyDBHelper;

public class IndexFragment extends Fragment {
    private FragmentIndexBinding mBinding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.mBinding = FragmentIndexBinding.inflate(inflater, container, false);
        return this.mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        StudentAdapter studentAdapter = new StudentAdapter();
        this.mBinding.rvStudent.setAdapter(studentAdapter);

        MyDBHelper helper = new MyDBHelper(getActivity());
        Set<Student> studentSet = helper.getAllStudents();
        for (Student student : studentSet) {
            studentAdapter.addStudent(student);
        }

        studentAdapter.setOnItemClickListener(student -> {
            Intent intent = new Intent(getActivity(), StudentDetailActivity.class);
            intent.putExtra("id", student.getId());
            intent.putExtra("name", student.getName());
            intent.putExtra("phone", student.getPhone());
            intent.putExtra("sex", student.getSex());
            intent.putExtra("birthday", student.getBirthday());
            startActivity(intent);
        });

        studentAdapter.setOnItemLongClickListener((v, student) -> {
            PopupMenu popup = new PopupMenu(getActivity(), v);
            popup.getMenuInflater().inflate(R.menu.long_click_menu, popup.getMenu());
            popup.setOnMenuItemClickListener(item -> {
                new MaterialAlertDialogBuilder(getActivity())
                        .setIcon(R.drawable.ic_prompt)
                        .setTitle("提示")
                        .setMessage("确认删除？")
                        .setPositiveButton("确定", (dialog, which) -> {
                            helper.deleteStudent(student.getId());
                        })
                        .setNegativeButton("取消", null)
                        .show();
                return true;
            });
            popup.show();
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        this.mBinding = null;
    }
}